<template>
	<view class="columnbox wdh-100 content">
		<view class="columnbox one-info" style="padding-bottom: 0;">
			<view class="rowbox wdh-100 jus  info-box" style="margin-top: 0;">
				<view class="info-title">姓名</view>
				<view>{{user.name}}</view>
			</view>
			<view class="rowbox wdh-100 jus info-box">
				<view class="info-title">性别</view>
				<view>{{user.sex==1?'男':'女'}}</view>
			</view>
			<view class="rowbox wdh-100 jus info-box">
				<view class="info-title">年龄</view>
				<view>{{age}}</view>
			</view>
		</view>
		<view class="columnbox one-info">
			<view class="rowbox wdh-100 jus  info-box" style="margin-top: 0;">
				<view class="info-title">诊断</view>
			</view>
			<view class="wdh-100 rowbox spb zd-box" @click="chooseDiagnosis">
				<view style="width: 500rpx;">{{diagnosis||'请输入诊断'}}</view>
				<u-icon name="arrow-right" size="24rpx"></u-icon>
			</view>
		</view>
		<view class="columnbox one-info">
			<view class="rowbox wdh-100 jus  info-box" style="margin-top: 0;">
				<view class="info-title">现病史</view>
			</view>
			<textarea v-model="xianbingshi" auto-height class="input-box" placeholder="请输入患者的症状,体征,性质以及持续时间"></textarea>
		</view>
		<view class="columnbox one-info">
			<view class="rowbox wdh-100 jus  info-box" style="margin-top: 0;">
				<view class="info-title">既往史</view>
			</view>
			<textarea v-model="jiwangshi" auto-height class="input-box" placeholder="请输入患者的过敏史,疾病史,手术外伤史等内容"></textarea>
		</view>
		<view class="columnbox one-info">
			<view class="rowbox wdh-100 jus  info-box" style="margin-top: 0;">
				<view class="info-title" style="width: 200rpx;">体检辅查</view>
			</view>
			<view class="wdh-100 rowbox spb zd-box" @click="addExam">
				<view v-if="examInfo==''" style="width: 500rpx;">点击添加</view>
				<view v-else class="tijian-list">
					<view v-if="examInfo.shengao!=''">身高：{{examInfo.shengao}}cm</view>
					<view v-if="examInfo.tizhong!=''">体重：{{examInfo.tizhong}}kg</view>
					<view v-if="examInfo.tiwen!=''">体温：{{examInfo.tiwen}}°C</view>
					<view v-if="examInfo.xinlu!=''">心率：{{examInfo.xinlu}}bpm</view>
					<view v-if="examInfo.shousuoya!=''">收缩压：{{examInfo.shousuoya}}mmHg</view>
					<view v-if="examInfo.shuzhangya!=''">舒张压：{{examInfo.shuzhangya}}mmHg</view>
				</view>
				<u-icon name="arrow-right" size="24rpx"></u-icon>
			</view>
		</view>
		<view class="columnbox one-info">
			<view class="rowbox wdh-100 spb  info-box" style="margin-top: 0;">
				<view class="info-title" style="width: 200rpx">处理意见</view>
				<view class="def-col" @click="showAdvice">使用模版</view>
			</view>
			<textarea v-model="adviceInfo" auto-height class="input-box" placeholder="请输入处理意见"></textarea>
		</view>
		<view style="width: 100%;height: 170rpx;"></view>
		<view class="bottom-box wdh-100 rowbox">
			<view v-if="submitflag" class="rowbox send-btn wdh-100" @click=""
				style="background: #f6f7fb;color:  #96949e;">发送给患者</view>
			<view v-else class="rowbox send-btn wdh-100" @click="submitInfo">发送给患者</view>
		</view>
	</view>
</template>

<script>
	// 环信即时通讯
	import SDK from 'js_sdk/easemob-im-uni-app/Easemob-chat';
	// 实例化 SDK 对象
	const WebIM = wx.WebIM = SDK;
	export default {
		data() {
			return {
				diagnosis: '', //诊断
				adviceInfo: '', //处理意见
				id: 0,
				user: '',
				age: 0,
				examInfo: '', //体检
				xianbingshi: '', //现病史
				jiwangshi: '', //既往史
				submitflag: false
			};
		},
		onLoad(options) {
			this.id = parseInt(options.id)
			this.getUserDetail()
		},
		methods: {
			//保存信息
			submitInfo() {
				if (this.diagnosis == '') {
					uni.showToast({
						title: '请输入诊断',
						icon: 'none'
					})
				}
				// else if (this.xianbingshi == '') {
				// 	uni.showToast({
				// 		title: '请输入现病史',
				// 		icon: 'none'
				// 	})
				// } else if (this.jiwangshi == '') {
				// 	uni.showToast({
				// 		title: '请输入既往史',
				// 		icon: 'none'
				// 	})
				// }
				// else if (this.examInfo == '') {
				// 	uni.showToast({
				// 		title: '请添加体检辅查',
				// 		icon: 'none'
				// 	})
				// }
				// else if (this.adviceInfo == '') {
				// 	uni.showToast({
				// 		title: '请添加处理意见',
				// 		icon: 'none'
				// 	})
				// }
				else {
					if (!this.submitflag) {
						this.submitflag = true
						uni.showLoading({
							title: '正在保存'
						})
						this.$req.post('/doctor/bingliadd', {
							doctorid: uni.getStorageSync('uid'),
							userid: this.id,
							name: this.user.name,
							sex: this.user.sex,
							age: this.age,
							zhenduan: this.diagnosis,
							xianbingshi: this.xianbingshi,
							jiwangshi: this.jiwangshi,
							shengao: this.examInfo == '' || this.examInfo.shengao == '' ? '' : this.examInfo
								.shengao * 1,
							tizhong: this.examInfo == '' || this.examInfo.tizhong == '' ? '' : this.examInfo
								.tizhong * 1,
							tiwen: this.examInfo == '' || this.examInfo.tiwen == '' ? '' : this.examInfo.tiwen * 1,
							xinlu: this.examInfo == '' || this.examInfo.xinlu == '' ? '' : this.examInfo.xinlu * 1,
							shousuoya: this.examInfo == '' || this.examInfo.shousuoya == '' ? '' : this.examInfo
								.shousuoya * 1,
							shuzhangya: this.examInfo == '' || this.examInfo.shuzhangya == '' ? '' : this.examInfo
								.shuzhangya * 1,
							yijian: this.adviceInfo
						}).then(res => {
							this.sendMessage(res.data.data, 11)
						})
					}
				}
			},
			sendMessage(content, classify) {
				let infocontent = {
					classify: classify,
					content: content + '',
					otherid: typeof(content) == 'number' ? content : 0, //开方ID
					name: this.user.name,
					chatroomheadpic: this.user.logo
				}
				let option = {
					type: "txt",
					msg: JSON.stringify(infocontent),
					to: this.user.memberid + "",
					chatType: "singleChat",
				};
				console.log(option)
				let msg = WebIM.message.create(option);
				getApp().globalData.conn.send(msg).then(res => {
					console.log("=========Send message success===返回内容==", res);
					setTimeout(() => {
						this.$store.dispatch('actionsHandleNewChatChange', {
							msg: JSON.stringify(infocontent),
							from: this.user.memberid + '',
							time: Date.parse(new Date),
							chatdetailflag: true,
							id: res.serverMsgId
						});
						uni.hideLoading()
						uni.navigateBack()
					}, 2000)
				}).catch((e) => {
					console.log("==============Send message fail", e);
				});
			},
			// 体检辅查
			addExam() {
				uni.navigateTo({
					url: '/pages/patient/exam'
				})
			},
			getUserDetail() {
				this.$req.post('/doctor/patientinfo', {
					userid: this.id
				}).then(res => {
					this.user = res.data.data.user
					this.getAge(res.data.data.user.shengri)
				})
			},
			getAge(birthday) {
				var today = new Date();
				var birthDate = new Date(birthday);
				var age = today.getFullYear() - birthDate.getFullYear();
				var monthDiff = today.getMonth() - birthDate.getMonth();
				if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
					age--;
				}
				this.age = age - 1;
			},
			//诊断
			chooseDiagnosis() {
				uni.navigateTo({
					url: '/pages/patient/diagnosis'
				})
			},
			//意见模版
			showAdvice() {
				uni.navigateTo({
					url: '/pages/patient/adviceList'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background: #F5F8FB;
		min-height: 100vh;
		justify-content: flex-start;
		font-family: PingFang SC;
	}

	.one-info {
		width: 710rpx;
		padding: 32rpx 40rpx;
		background: #fff;
		border-radius: 20rpx;
		margin-top: 20rpx;
		font-size: 28rpx;
	}

	.info-box {
		font-size: 28rpx;
		border-bottom: #f6f7fb solid 1px;
		padding-bottom: 32rpx;
		margin-top: 20rpx;
		color: #202020;
	}

	.info-title {
		color: #333333;
		width: 90rpx;
	}

	.one-info>view:last-child {
		border: none;
	}

	.zd-box {
		margin-top: 32rpx;
		font-size: 28rpx;
	}

	.input-box {
		width: 100%;
		font-size: 28rpx;
		margin-top: 20rpx;
	}

	.tijian-list {
		width: 600rpx;
		display: flex;
		flex-wrap: wrap;
		justify-content: flex-start;

		view {
			padding: 10rpx 20rpx;
			color: #00b6b2;
			background: rgba(#00b6b2, .07);
			font-size: 28rpx;
			line-height: 40rpx;
			border-radius: 100rpx;
			margin-right: 20rpx;
			width: fit-content;
			margin-bottom: 10rpx;
		}
	}
</style>